from django.shortcuts import render
from django.http import HttpResponse
from .models import total_unemployment
from django.core import serializers
import json
from django.http import JsonResponse
from django.forms.models import model_to_dict


# Create your views here.
def index(request):
    return HttpResponse("Hello, world. You're at the echarts index.")

 
def home(request):
    COUNTRY_NAME_LIST = total_unemployment.objects.order_by('country_code')
    context          = {}
    context['hello'] = 'Hello World!'
    context['COUNTRY_NAME_LIST'] = COUNTRY_NAME_LIST
    return render(request, 'index.html', context)

def load(request):
    xxxx = request.POST.get('id', 0)
    data = total_unemployment.objects.get(id=xxxx)
    data = model_to_dict(data)
    return JsonResponse(data)

def search(request):
    keywords = request.POST.get('keywords', '')
    data = total_unemployment.objects.filter(country_name__contains=keywords)
    data = list(data.values())
    return JsonResponse(data, safe=False)